package it.globalgamejam.scar.game.world 
{
	import it.globalgamejam.scar.Assets;
	import it.globalgamejam.scar.game.GameWorld;
	import net.flashpunk.Entity;
	import net.flashpunk.FP;
	import net.flashpunk.graphics.Image;
	import net.flashpunk.graphics.Spritemap;
	import net.flashpunk.Sfx;
	/**
	 * ...
	 * @author N1xx1
	 */
	public class Rock extends Entity
	{
		
		private var speedX:Number = 0;
		private var speedY:Number = 0;
		
		private var moving:Boolean = false;
		private var movingTimer:Number = 0;
		
		private var spritemap:Spritemap = new Spritemap(Assets.ROCK, 51, 51);
		
		public function Rock(x:Number=0, y:Number=0) 
		{
			super(x, y);
			
			spritemap.add("idle", [0], 20, true);
			spritemap.add("move", [0, 1, 2], 10, true);
			spritemap.originX = 25;
			spritemap.originY = 25;
			spritemap.x = 25;
			spritemap.y = 25;
			graphic = spritemap;
			
			setHitbox(50, 50);
			
			layer = 40;
		}
		
		public function setSpawnPos(_x:Number, _y:Number):void {
			x = _x + 35;
			y = _y + 35;
		}
		
		public static function createRock():Rock {
			return new Rock();
		}
		
		public override function update():void 
		{
			super.update();
			
			if (GameWorld(FP.world).isPlaying) {
				
				var mouse:Mouse = collideTypes("mouse", x, y) as Mouse;
				if (mouse) {
					if(!mouse.blocked) {
						mouse.blockHole();
						FP.world.remove(this);
						GameWorld(FP.world).sfxRoll2.stop();
						return;
					}
				}
				
				var player:Player = FP.world.getInstance("player") as Player;
				if (player) {
					if (collideWith(player, x, y)) {
						player.deltaSpeed = 10;
						if (player.speedY < -100) player.speedY = -100;
						if (player.speedY >  100) player.speedY = 100;
						if (player.speedX < -100) player.speedX = -100;
						if (player.speedX >  100) player.speedX = 100;
						
						var dx:Number = player.x + player.width/2 - (x + width/2);
						var dy:Number = player.y + player.height/2 - (y + height/2);
						var d:Number = Math.max(Math.abs(dx), Math.abs(dy));
						
						if (d >= 1) {
							d = Math.sqrt(d);
							dx /= d;
							dy /= d;
							
							var d1:Number = 1 / d;
							if (d1 > 1) d1 = 1;
							
							dx *= d1;
							dy *= d1;
							dx *= 28;
							dy *= 28;

							speedX += -dx;
							player.speedX += dx;
							speedY += -dy;
							player.speedY += dy;
						}
					} else {
						player.deltaSpeed = 20;
					}
				}
				
				var ball:Ball = FP.world.getInstance("ball") as Ball;
				if (ball) {
					if (collideWith(ball, x, y)) {
						var dx1:Number = ball.x + ball.width/2 - (x + width/2);
						var dy1:Number = ball.y + ball.height/2 - (y + height/2);
						var d_1:Number = Math.max(Math.abs(dx1), Math.abs(dy1));
						
						if (d_1 >= 1) {
							d_1 = Math.sqrt(d_1);
							dx1 /= d_1;
							dy1 /= d_1;
							
							var d_2:Number = 1 / d_1;
							if (d_2 > 1) d_2 = 1;
							
							dx1 *= d_2;
							dy1 *= d_2;
							dx1 *= 28;
							dy1 *= 28;
							
							speedX += -dx1;
							speedY += -dy1;
							if (Math.abs(dx) > Math.abs(dy)) {
								ball.speedX += dx1;
							} else {
								ball.speedY += dy1;
							}
						}
					}
				}
				
				speedX *= 0.95;
				speedY *= 0.95;
				
				var speedXabs:Number = Math.abs(speedX);
				var speedYabs:Number = Math.abs(speedY);
				
				if (speedXabs < 4) speedX = 0;
				if (speedYabs < 4) speedY = 0;
				
				if (speedXabs < speedYabs) {
					spritemap.angle = 0;
				} else if (speedXabs != 0 || speedYabs != 0) {
					spritemap.angle = 90;
				}
				
				if (speedXabs > 3 || speedYabs > 3) {
					if (!moving) {
						spritemap.play("move");
						moving = true;
						GameWorld(FP.world).sfxRoll2.loop();
					}
					movingTimer = 0;
				} else if(movingTimer > 0.5 && moving) {
					spritemap.play("idle");
					moving = false;
					GameWorld(FP.world).sfxRoll2.stop();
				} else if(moving) {
					movingTimer += FP.elapsed;
				}
				moveBy(speedX * FP.elapsed, speedY * FP.elapsed, "wall");
			}
		}
		
		override public function moveCollideX(e:Entity):Boolean
		{
			speedX = 0;
			return true;
		}
		
		override public function moveCollideY(e:Entity):Boolean
		{
			speedY = 0;
			return true;
		}
	}

}